﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace fbasync
{
    internal static class Log
    {
        //TODO: move this to centralized logging
        private static volatile bool _eventLogExists = false;

        [DebuggerHidden]
        private static void CreateSourceIfNecessary()
        {
            if (!_eventLogExists)
            {
                _eventLogExists = EventLog.SourceExists("fbasync");
                if (!_eventLogExists)
                {
                    EventLog.CreateEventSource(new EventSourceCreationData("fbasync", "Application"));
                    _eventLogExists = true;
                }
            }
        }

        [DebuggerHidden]
        public static void WriteWarning(Exception ex, string description)
        {
            try
            {
                Trace.WriteLine("Asynchronous exception: " + ex.ToString());
                CreateSourceIfNecessary();
                EventLog.WriteEntry("fbAsync", description + ex.ToString(), EventLogEntryType.Warning);
            }
            catch (Exception innerEx)
            {
                Trace.WriteLine("Error writing event log entry: " + innerEx.ToString());
            }
        }
    }
}
